
package com.amigo.online.provider.manager.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.amigo.online.common.base.service.BaseService;
import com.amigo.online.provider.manager.mapper.AdvNearbyApplyTblMapper;
import com.amigo.online.provider.manager.mapper.AdvNearbyAreaTblMapper;
import com.amigo.online.provider.manager.mapper.AdvNearbyStockTblMapper;
import com.amigo.online.provider.manager.model.entity.AdvAreaTbl;
import com.amigo.online.provider.manager.model.entity.AdvNearbyApplyTbl;
import com.amigo.online.provider.manager.model.entity.AdvNearbyAreaTbl;
import com.amigo.online.provider.manager.model.entity.AdvNearbyStockTbl;
import com.amigo.online.provider.manager.result.AdvNearbyInfo;
import com.amigo.online.provider.manager.service.AdvNearbyApplyTblService;
import com.amigo.online.provider.manager.util.OrderUtils;

/**
 * <code>AdvNearbyApplyTblServiceImlp.java</code>
 * <p>
 * : 附近广告服务
 * </p>
 * <p>
 *
 * @author LuChen
 * @version 1.0
 * @company Amigo
 * @time 2018年11月26日 下午6:40:12
 * @lastUpdate 无
 * </p>
 */

@Service
@Transactional(rollbackFor = Exception.class)
public class AdvNearbyApplyTblServiceImpl extends BaseService<AdvNearbyApplyTbl> implements AdvNearbyApplyTblService {

    @Autowired
    private AdvNearbyApplyTblMapper advNearbyApplyTblMapper;

    @Autowired
    private AdvNearbyStockTblMapper advNearbyStockTblMapper;

    @Autowired
    private AdvNearbyAreaTblMapper advNearbyAreaTblMapper;


    @Override
    public AdvNearbyApplyTbl addAdvNearbyApply(AdvNearbyApplyTbl advNearbyApplyTbl) {
        String orderId = OrderUtils.createOrderNumber();
        advNearbyApplyTbl.setOrderId(orderId);
        Long nowTime = System.currentTimeMillis() / 1000;
        advNearbyApplyTbl.setCreatedTime(nowTime);
        advNearbyApplyTbl.setStatus(0);
        advNearbyApplyTbl.setDeleted(0);
        if (advNearbyApplyTblMapper.insertUseGeneratedKeys(advNearbyApplyTbl) > 0) {
            List<Long> areaId = advNearbyApplyTbl.getAreaId();
            Long advId = advNearbyApplyTbl.getId();
            if (areaId != null && areaId.size() > 0) {
                List<AdvNearbyAreaTbl> list = new ArrayList<>();
                for (Long area : areaId) {
                    AdvNearbyAreaTbl e = new AdvNearbyAreaTbl();
                    e.setAdvId(advId);
                    e.setAreaId(area);
                    list.add(e);
                }
                if (advNearbyAreaTblMapper.insertList(list) > 0) {
                    return advNearbyApplyTbl;
                }
            } else {
                return null;
            }
        }
        return null;
    }


    @Override
    public boolean updateAdvNearbyApply(AdvNearbyApplyTbl advNearbyApplyTbl) {
        AdvNearbyApplyTbl selectById = advNearbyApplyTblMapper.selectById(advNearbyApplyTbl.getId());
        if (selectById.getStatus() == null || selectById.getStatus().equals(1)) {
            return false;
        }
        if (advNearbyApplyTblMapper.updateById(advNearbyApplyTbl) > 0) {
            List<Long> areaId = advNearbyApplyTbl.getAreaId();
            if (areaId != null && areaId.size() > 0) {
                Long advId = advNearbyApplyTbl.getId();
                AdvNearbyAreaTbl ex = new AdvNearbyAreaTbl();
                ex.setAdvId(advId);
                advNearbyAreaTblMapper.delete(ex);
                List<AdvNearbyAreaTbl> list = new ArrayList<>();
                for (Long area : areaId) {
                    AdvNearbyAreaTbl e = new AdvNearbyAreaTbl();
                    e.setAdvId(advId);
                    e.setAreaId(area);
                    list.add(e);
                }
                if (advNearbyAreaTblMapper.insertList(list) > 0) {
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public boolean deleteById(AdvNearbyApplyTbl advNearbyApplyTbl) {
        Long deleteTime = System.currentTimeMillis() / 1000;
        advNearbyApplyTbl.setDeletedTime(deleteTime);
        if (advNearbyApplyTblMapper.deletebyId(advNearbyApplyTbl) > 0) {
            return true;
        }
        return false;
    }


    @Override
    public boolean checkAdv(AdvNearbyApplyTbl advNearbyApplyTbl) {
        Long id = advNearbyApplyTbl.getId();
        advNearbyApplyTblMapper.updateById(advNearbyApplyTbl);
        if (advNearbyApplyTbl.getStatus() == 1) {
            AdvNearbyApplyTbl adv = advNearbyApplyTblMapper.selectById(id);
            AdvNearbyStockTbl advStock = new AdvNearbyStockTbl();
            advStock.setAdvId(adv.getId());
            advStock.setApplyNumber(adv.getTotalNumber());
            advStock.setRemainingNumber(adv.getTotalNumber());
            advStock.setOverUsedNumber(0);
            advStock.setUsedNumber(0);
            if (advNearbyStockTblMapper.insert(advStock) > 0) {
                return true;
            } else {
                return false;
            }
        }
        return true;
    }


    @Override
    public JSONObject findNearbyAdv(AdvNearbyApplyTbl advNearbyApplyTbl) {
        if (advNearbyApplyTbl.getPageNo() == null || advNearbyApplyTbl.getPageNo() < 1) {
            advNearbyApplyTbl.setPageNo(1);
        }
        if (advNearbyApplyTbl.getPageCount() == null || advNearbyApplyTbl.getPageCount() < 1) {
            advNearbyApplyTbl.setPageCount(10);
        }
        int totalCount = advNearbyApplyTblMapper.findNearbyAdvCount(advNearbyApplyTbl);
        int totalPage = totalCount % advNearbyApplyTbl.getPageCount() == 0 ? totalCount / advNearbyApplyTbl.getPageCount() : totalCount / advNearbyApplyTbl.getPageCount() + 1;
        int pageStart = (advNearbyApplyTbl.getPageNo() - 1) * advNearbyApplyTbl.getPageCount();
        advNearbyApplyTbl.setPageStart(pageStart);
        List<AdvNearbyApplyTbl> list = advNearbyApplyTblMapper.findNearbyAdv(advNearbyApplyTbl);
        JSONObject re = new JSONObject();
        re.put("list", list);
        re.put("totalCount", totalCount);
        re.put("totalPage", totalPage);
        re.put("pageNo", advNearbyApplyTbl.getPageNo());
        re.put("pageCount", advNearbyApplyTbl.getPageCount());
        return re;
    }


    @Override
    public AdvNearbyInfo findNearbyAdvDetailInfo(Long id) {
        AdvNearbyInfo info = advNearbyApplyTblMapper.findNearbyAdvDetailInfo(id);
        List<AdvAreaTbl> areaInfo = advNearbyApplyTblMapper.findNearbyAdvDetailAreaInfo(id);
        info.setAreaList(areaInfo);
        return info;
    }
}

